package com.jiantong.web.util;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import org.springframework.stereotype.Service;

/**
 * Created by mz on 28/7/17.
 *
 */
@Service
public class SendMessage {

    private static final String product = "Dysmsapi";
    //产品域名,开发者无需替换
    private static final String domain = "dysmsapi.aliyuncs.com";

    // TODO 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找)
    private static final String accessKeyId = "UU60NderpiWvL8pO";
    private static final String accessKeySecret = "XnEReA2mIHlDQEeQjAshEtODhvV78c";


    private String name;

    private String number;

    private String declare_stations;

    private String mobile;


//    public SendMessage(String name, String number, String declare_stations, String mobile) {
//        this.name = name;
//        this.number = number;
//        this.declare_stations = declare_stations;
//        this.mobile = mobile;
//    }

    public Boolean sendMsg(String name, String number, String declare_stations, String mobile) {
        this.name = name;
        this.number = number;
        this.declare_stations = declare_stations;
        this.mobile = mobile;

        try {
            IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);

            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
            IAcsClient acsClient = new DefaultAcsClient(profile);

            //组装请求对象-具体描述见控制台-文档部分内容
            SendSmsRequest request = new SendSmsRequest();
            //必填:待发送手机号
            request.setPhoneNumbers(this.mobile);
            //必填:短信签名-可在短信控制台中找到
            request.setSignName("动检通");
            //必填:短信模板-可在短信控制台中找到
            request.setTemplateCode("SMS_99095015");

            request.setTemplateParam("{\"name\":\"" + this.name + "\", \"number\":\"" + this.number + "\",\"certificate_station\":\"" + this.declare_stations + "\"}");

            SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);

            if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
                return Boolean.TRUE;
            }
        }catch (ClientException e)
        {
            return Boolean.FALSE;
        }

        return Boolean.FALSE;
    }


}
